﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;


namespace MetroLightKit.ConvertSvg
{
    public class MessageWorkerArgs : EventArgs
    {
        public TypeMessage Type { get; set; }
        public string Message { get; set; }
        public int? Percent { get; set; }
        public MessageWorkerArgs()
        {
        }
        public MessageWorkerArgs(TypeMessage type, string format, params object[] parametres)
        {
            this.Type = type;
            this.Message = string.Format(format, parametres);
        }
        public MessageWorkerArgs(TypeMessage type, int? percent, string format, params object[] parametres)
        {
            this.Type = type;
            this.Message = string.Format(format, parametres);

            if (percent < 1)
                this.Percent = 1;
            else if (percent > 100)
                this.Percent = 99;
            else
                this.Percent = percent;
        }
    }
    public enum TypeMessage
    {
        Information,
        Start,
        End,
        EndError,
        Cancel,
        Error
    }
    public static class Extension
    {
        public static long Length(this DirectoryInfo di)
        {
            return di.GetFiles("*", System.IO.SearchOption.AllDirectories).Sum(file => file.Length);
        }
    } 
    public class WebSite
    {
        public string Url { get; set; }
        public string XpathSvgs { get; set; }
        public string XpathName { get; set; }

        private static readonly Regex RegConstruct = new Regex(@"(?<URL>[^\t\s]+)[\t\s]+(?<SVG>[^\t\s]+)[\t\s]+(?<NAME>[^\t\s]+)");
        private static readonly Regex RegCounter = new Regex(@"(?<LURL>[^\[]+)\[(?<START>[\d]+)-(?<END>[\d]+)](?<RURL>.*)");
        public IEnumerable<string> GetUrls()
        {
            var match = RegCounter.Match(Url);
            if (match.Success)
            {
                int start = 0;
                int end = 0;

                int.TryParse(match.Groups["START"].Value, out start);
                int.TryParse(match.Groups["END"].Value, out end);
                var debut = match.Groups["LURL"].Value;
                var fin = match.Groups["RURL"].Value;

                for (int i = start; i <= end; i++)
                {
                    yield return String.Format("{0}{1}{2}", debut, i, fin);
                }
            }
            else
            {
                yield return Url;
            }
        }
        public WebSite(string comboStr)
        {
            var match = RegConstruct.Match(comboStr);
            if (match.Success)
            {
                if (match.Groups["URL"].Success && match.Groups["SVG"].Success && match.Groups["NAME"].Success)
                {
                    Url = match.Groups["URL"].Value;
                    XpathSvgs = match.Groups["SVG"].Value;
                    XpathName = match.Groups["NAME"].Value;
                }
            }
        }
        public override string ToString()
        {
            return Url;
        }
        public bool Equals(string url)
        {
            return Url.Equals(url);
        }
        public override bool Equals(object obj)
        {
            return Equals(obj.ToString());
        }
    }
}
